package com.xyzh.application.base.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xyzh.application.base.entity.UserEntity;
import com.xyzh.application.base.vo.AuthorUser;
import com.xyzh.application.config.security.myPasswordEncoder;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import com.xyzh.application.base.dao.UserMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author @JZB
 * @since 2022-11-01
 */
@Service
public class UserServiceImpl extends BasicServiceImpl<UserMapper, UserEntity> implements IUserService, UserDetailsService {




    @Override
    public UserDetails loadUserByUsername(String username) {
        AuthorUser user = new AuthorUser();
        try {
            user = queryUserInfo(username);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }

        return user;
    }


    /**
     * 查询用户信息，包含用户角色权限等
     *
     * @param username
     * @return
     */
    private AuthorUser queryUserInfo(String username) {
        QueryWrapper<UserEntity> query = new QueryWrapper<>();
        query.eq("username", username);
        UserEntity user = this.getBaseMapper().selectOne(query);
        if (ObjectUtil.isNull(user)) {
            throw new RuntimeException("系统中不存在该用户");
        }
        AuthorUser au = new AuthorUser();
        BeanUtils.copyProperties(user, au);
        List<GrantedAuthority> list = new ArrayList<>();
        au.setAuthorities(list);
        return au;
    }

    public UserDetails loadUserByOpenId(String openid) {
        AuthorUser user = new AuthorUser();
        try {
            QueryWrapper<UserEntity> query = new QueryWrapper<>();
            query.eq("openid", openid);
            UserEntity userEntity = this.getBaseMapper().selectOne(query);
            BeanUtils.copyProperties(userEntity, user);
            List<GrantedAuthority> list = new ArrayList<>();
            user.setAuthorities(list);
        } catch (Exception e) {

        }
        return user;
    }

    @Override
    public UserEntity queryUserEntityByOpenid(String openId) {
        QueryWrapper<UserEntity> query = new QueryWrapper<>();
        query.eq("openid", openId);
        return this.getBaseMapper().selectOne(query);
    }


    @Override
    public boolean updateEntity(UserEntity entity) {
        if (StrUtil.isNotEmpty(entity.getPassword())) {
            PasswordEncoder passwordEncoder = new myPasswordEncoder();
            entity.setPassword(passwordEncoder.encode(entity.getPassword()));
        }
        return super.updateEntity(entity);
    }


}
